<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Builder;

class DepotBuilder extends Builder
{
    public function __construct($query, $filter = true, $depot_id_key)
    {
        parent::__construct($query);

        if($filter){
            // $request = App::make('request');
            // $this->applyFilters($request->attributes->all(), $depot_id_key);
        }
    }

    /**
     * Apply filters to the query based on the provided parameters.
     *
     * @param array $params
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function applyFilters(array $params, $depot_id_key)
    {
        $auth = $params['auth'] ?? null;

        if($auth && $auth->depot_ids) {
            if($auth->depot_ids == ['*']) {
                return $this;
            }
            //供应商和司机,只能看到自己所属网点的内容
            if($auth->type == AdminModel::SUPPLIER_TYPE || $auth->type == AdminModel::DRIVER_TYPE){
                $this->where($depot_id_key, $auth->depot_id);
                return $this;
            }

            if($auth->depot_ids != ['*']){
                $this->whereIn($depot_id_key, $auth->depot_ids);
                return $this;
            }
        }

        return $this;
    }
}